package service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;



import pojo.table;
import pojo.TableType;




import base.BaseService;

public class TableService extends BaseService {
	public class TableRowMapper implements RowMapper<table> {
		
	  	   public table mapRow(ResultSet rs, int rownum) throws SQLException {
	  
		table inf = new table();
		inf.setId(rs.getInt("id"));
		inf.setArea(rs.getInt("area"));
		inf.setType(rs.getInt("type")); 
		inf.setName(rs.getString("name"));
        inf.setOrderAM(rs.getInt("orderAM"));
        inf.setOrderPM(rs.getInt("orderPM"));
		inf.setDetail(rs.getString("detail"));
		return inf;
		
}
	}
	//获取餐桌总数
	public int getALL(String where){
		String sql = "SELECT count(*) FROM tableinfor "+where+"";
		
		int count1 = this.jdbcTemplate.queryForInt(sql);
		
		return count1;
		
	}
	//餐桌列表
	public List<table> getTable(int pageno,int pagesize,String where){
		String sql = "SELECT * from tableinfor "+where+" limit "+(pageno-1)*pagesize+","+pagesize;
		return jdbcTemplate.query(sql,new TableRowMapper());
}  //根据id获取餐桌信息
	public table getTableDetail(int id){
		
		String sql="select * from tableinfor where id=?";
		table de = null;
		
		try{
			de= jdbcTemplate.queryForObject(sql, new TableRowMapper(),id);
		
		}catch(EmptyResultDataAccessException e){
			
			}

		return de;
	}//根据id修改餐桌信息
	public void getUpdateByid(int area,int type,String name,String detail,int id){
		
		String sql ="update tableinfor set area=?,type=?,name=?,detail=? where id=?";
		
		 jdbcTemplate.update(sql,area,type,name,detail,id);
		}	//删除
	 public int DeleteTable(int id){
			
			String sql ="delete from tableinfor where id=?";
			
			return jdbcTemplate.update(sql,id);
			
	 }//餐桌类型列表	
	 public List<TableType> getTableType(){
		 class RowMapper implements org.springframework.jdbc.core.RowMapper<TableType> {
			 public TableType mapRow(ResultSet rs, int rownum) throws SQLException {

				 TableType dt = new TableType();
					dt.setId(rs.getInt("id"));
					dt.setName(rs.getString("name"));
									
					return dt;
							}
					}			
			String sql = "SELECT * from tabletype ";
			return jdbcTemplate.query(sql,new RowMapper());
			
	}
	 //根据id获得餐桌类型信息
	 public TableType getType(int id){
		 class RowMapper implements org.springframework.jdbc.core.RowMapper<TableType> {
			 public TableType mapRow(ResultSet rs, int rownum) throws SQLException {

				 TableType dt = new TableType();
					dt.setId(rs.getInt("id"));
					dt.setName(rs.getString("name"));
									
					return dt;
							}
					}			
			String sql = "SELECT * from tabletype where id=?";
			return jdbcTemplate.queryForObject(sql,new RowMapper(),id);
	 }
			//根据id修改餐桌信息
			public void UpadateTypeInfor(String name,int id){
				
				String sql ="update tabletype set name=? where id=?";
				
				 jdbcTemplate.update(sql,name,id);
				}
			//删除餐桌类型
			 public int DeleteType(int id){
					
					String sql ="delete from tabletype where id=?";
					
					return jdbcTemplate.update(sql,id);
					
			 }	
			 //添加餐桌类型
			 public int addTableType(String name ){
					
					String sql ="insert into tabletype set name=?";
					
					return jdbcTemplate.update(sql,name);
				}
	 //添加餐桌信息
	 public int addTable(int area,int type,String name,String detail ){
			
			String sql ="insert into tableinfor(area,type,name,detail) values(?,?,?,?)";
			
			return jdbcTemplate.update(sql,area,type,name,detail);
		}
	 public int getTableNum(String date,int tablearea,int tabletype,int type){
		 String sql = "SELECT count(*) FROM `order` where checkinTime like '"+date+"' and tableArea="+tablearea+" and tableType="+tabletype+" and type="+type+" and tradeStatus in (1,2,3)";
			
			int count1 = this.jdbcTemplate.queryForInt(sql);
			
			return count1;
	 }
	 public int getTotalTable(int tablearea,int tabletype){
		 String sql = "SELECT count(*) FROM tableinfor where area="+tablearea+" and type="+tabletype;
			
			int count1 = this.jdbcTemplate.queryForInt(sql);
			
			return count1;
		 
	 }

}
